<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Getting started</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('GettingStarted.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Getting started </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This is a very short guide on how to get started with <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>. It has a dual purpose. It serves as a minimal introduction to the <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> library for people who want to start coding as soon as possible. You can also read this page as the first part of the Tutorial, which explains the library in more detail; in this case you will continue with <a class="el" href="group__TutorialMatrixClass.html">The Matrix class</a>.</p>
<h1><a class="anchor" id="GettingStartedInstallation"></a>
How to "install" Eigen?</h1>
<p>In order to use <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>, you just need to download and extract <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>'s source code (see <a href="http://eigen.tuxfamily.org/index.php?title=Main_Page#Download">the wiki</a> for download instructions). In fact, the header files in the <code><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a></code> subdirectory are the only files required to compile programs using <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>. The header files are the same for all platforms. It is not necessary to use CMake or install anything.</p>
<h1><a class="anchor" id="GettingStartedFirstProgram"></a>
A simple first program</h1>
<p>Here is a rather simple program to get you started.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">Eigen::MatrixXd</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a> m(2,2);</div>
<div class="line">  m(0,0) = 3;</div>
<div class="line">  m(1,0) = 2.5;</div>
<div class="line">  m(0,1) = -1;</div>
<div class="line">  m(1,1) = m(1,0) + m(0,1);</div>
<div class="line">  std::cout &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga99b41a69f0bf64eadb63a97f357ab412"><div class="ttname"><a href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">Eigen::MatrixXd</a></div><div class="ttdeci">Matrix&lt; double, Dynamic, Dynamic &gt; MatrixXd</div><div class="ttdoc">Dynamic×Dynamic matrix of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment --><p>We will explain the program after telling you how to compile it.</p>
<h1><a class="anchor" id="GettingStartedCompiling"></a>
Compiling and running your first program</h1>
<p>There is no library to link to. The only thing that you need to keep in mind when compiling the above program is that the compiler must be able to find the <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> header files. The directory in which you placed <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>'s source code must be in the include path. With GCC you use the <code>-I</code> option to achieve this, so you can compile the program with a command like this:</p>
<div class="fragment"><div class="line">g++ -I /path/to/eigen/ my_program.cpp -o my_program </div>
</div><!-- fragment --><p>On Linux or Mac OS X, another option is to symlink or copy the <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> folder into <code>/usr/local/include/</code>. This way, you can compile the program with:</p>
<div class="fragment"><div class="line">g++ my_program.cpp -o my_program </div>
</div><!-- fragment --><p>When you run the program, it produces the following output:</p>
<div class="fragment"><div class="line">  3  -1</div>
<div class="line">2.5 1.5</div>
</div><!-- fragment --><h1><a class="anchor" id="GettingStartedExplanation"></a>
Explanation of the first program</h1>
<p>The <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> header files define many types, but for simple applications it may be enough to use only the <code>MatrixXd</code> type. This represents a matrix of arbitrary size (hence the <code>X</code> in <code>MatrixXd</code>), in which every entry is a <code>double</code> (hence the <code>d</code> in <code>MatrixXd</code>). See the <a class="el" href="group__QuickRefPage.html#QuickRef_Types">quick reference guide</a> for an overview of the different types you can use to represent a matrix.</p>
<p>The <code>Eigen/Dense</code> header file defines all member functions for the MatrixXd type and related types (see also the <a class="el" href="group__QuickRefPage.html#QuickRef_Headers">table of header files</a>). All classes and functions defined in this header file (and other <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> header files) are in the <code><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a></code> namespace.</p>
<p>The first line of the <code>main</code> function declares a variable of type <code>MatrixXd</code> and specifies that it is a matrix with 2 rows and 2 columns (the entries are not initialized). The statement <code>m(0,0) = 3</code> sets the entry in the top-left corner to 3. You need to use round parentheses to refer to entries in the matrix. As usual in computer science, the index of the first index is 0, as opposed to the convention in mathematics that the first index is 1.</p>
<p>The following three statements sets the other three entries. The final line outputs the matrix <code>m</code> to the standard output stream.</p>
<h1><a class="anchor" id="GettingStartedExample2"></a>
Example 2: Matrices and vectors</h1>
<p>Here is another example, which combines matrices with vectors. Concentrate on the left-hand program for now; we will talk about the right-hand program later.</p>
<table class="manual">
<tr>
<th>Size set at run time:</th><th>Size set at compile time: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">Eigen::MatrixXd</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">Eigen::VectorXd</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a> m = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXd::Random</a>(3,3);</div>
<div class="line">  m = (m + <a class="code" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">MatrixXd::Constant</a>(3,3,1.2)) * 50;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;m =&quot;</span> &lt;&lt; std::endl &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">VectorXd</a> v(3);</div>
<div class="line">  v &lt;&lt; 1, 2, 3;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;m * v =&quot;</span> &lt;&lt; std::endl &lt;&lt; m * v &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a68a7ece6c5629d1e9447a321fcb14ccd"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">Eigen::DenseBase::Constant</a></div><div class="ttdeci">static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &amp;value)</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:191</div></div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_ae814abb451b48ed872819192dc188c19"><div class="ttname"><a href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::DenseBase::Random</a></div><div class="ttdeci">static const RandomReturnType Random()</div><div class="ttdef"><b>Definition:</b> Random.h:114</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga8554c6170729f01c7572574837ecf618"><div class="ttname"><a href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">Eigen::VectorXd</a></div><div class="ttdeci">Matrix&lt; double, Dynamic, 1 &gt; VectorXd</div><div class="ttdoc">Dynamic×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga84e9fd068879d808012bb6d5dbfecb17">Eigen::Matrix3d</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#gaabb0b4639dc0b48e691e02e95873b0f1">Eigen::Vector3d</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga84e9fd068879d808012bb6d5dbfecb17">Matrix3d</a> m = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Matrix3d::Random</a>();</div>
<div class="line">  m = (m + <a class="code" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">Matrix3d::Constant</a>(1.2)) * 50;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;m =&quot;</span> &lt;&lt; std::endl &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#gaabb0b4639dc0b48e691e02e95873b0f1">Vector3d</a> v(1,2,3);</div>
<div class="line">  </div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;m * v =&quot;</span> &lt;&lt; std::endl &lt;&lt; m * v &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga84e9fd068879d808012bb6d5dbfecb17"><div class="ttname"><a href="group__matrixtypedefs.html#ga84e9fd068879d808012bb6d5dbfecb17">Eigen::Matrix3d</a></div><div class="ttdeci">Matrix&lt; double, 3, 3 &gt; Matrix3d</div><div class="ttdoc">3×3 matrix of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaabb0b4639dc0b48e691e02e95873b0f1"><div class="ttname"><a href="group__matrixtypedefs.html#gaabb0b4639dc0b48e691e02e95873b0f1">Eigen::Vector3d</a></div><div class="ttdeci">Matrix&lt; double, 3, 1 &gt; Vector3d</div><div class="ttdoc">3×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment --> </td></tr>
</table>
<p>The output is as follows:</p>
<div class="fragment"><div class="line">m =</div>
<div class="line">  94 89.8 43.5</div>
<div class="line">49.4  101 86.8</div>
<div class="line">88.3 29.8 37.8</div>
<div class="line">m * v =</div>
<div class="line">404</div>
<div class="line">512</div>
<div class="line">261</div>
</div><!-- fragment --><h1><a class="anchor" id="GettingStartedExplanation2"></a>
Explanation of the second example</h1>
<p>The second example starts by declaring a 3-by-3 matrix <code>m</code> which is initialized using the <a class="el" href="classEigen_1_1DenseBase.html#ae97f8d9d08f969c733c8144be6225756">Random() </a> method with random values between -1 and 1. The next line applies a linear mapping such that the values are between 10 and 110. The function call <a class="el" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">MatrixXd::Constant</a>(3,3,1.2) returns a 3-by-3 matrix expression having all coefficients equal to 1.2. The rest is standard arithmetic.</p>
<p>The next line of the <code>main</code> function introduces a new type: <code>VectorXd</code>. This represents a (column) vector of arbitrary size. Here, the vector <code>v</code> is created to contain <code>3</code> coefficients which are left uninitialized. The one but last line uses the so-called comma-initializer, explained in <a class="el" href="group__TutorialAdvancedInitialization.html">Advanced initialization</a>, to set all coefficients of the vector <code>v</code> to be as follows:</p>
<p class="formulaDsp">
\[ v = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}. \]
</p>
<p>The final line of the program multiplies the matrix <code>m</code> with the vector <code>v</code> and outputs the result.</p>
<p>Now look back at the second example program. We presented two versions of it. In the version in the left column, the matrix is of type <code>MatrixXd</code> which represents matrices of arbitrary size. The version in the right column is similar, except that the matrix is of type <code>Matrix3d</code>, which represents matrices of a fixed size (here 3-by-3). Because the type already encodes the size of the matrix, it is not necessary to specify the size in the constructor; compare <code>MatrixXd m(3,3)</code> with <code>Matrix3d m</code>. Similarly, we have <code>VectorXd</code> on the left (arbitrary size) versus <code>Vector3d</code> on the right (fixed size). Note that here the coefficients of vector <code>v</code> are directly set in the constructor, though the same syntax of the left example could be used too.</p>
<p>The use of fixed-size matrices and vectors has two advantages. The compiler emits better (faster) code because it knows the size of the matrices and vectors. Specifying the size in the type also allows for more rigorous checking at compile-time. For instance, the compiler will complain if you try to multiply a <code>Matrix4d</code> (a 4-by-4 matrix) with a <code>Vector3d</code> (a vector of size 3). However, the use of many types increases compilation time and the size of the executable. The size of the matrix may also not be known at compile-time. A rule of thumb is to use fixed-size matrices for size 4-by-4 and smaller.</p>
<h1><a class="anchor" id="GettingStartedConclusion"></a>
Where to go from here?</h1>
<p>It's worth taking the time to read the <a class="el" href="group__TutorialMatrixClass.html">long tutorial</a>.</p>
<p>However if you think you don't need it, you can directly use the classes documentation and our <a class="el" href="group__QuickRefPage.html">Quick reference guide</a>.</p>
<ul>
<li><b>Next:</b> <a class="el" href="group__TutorialMatrixClass.html">The Matrix class</a> </li>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
